<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <title>人员-权限管理</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-store, must-revalidate">
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta http-equiv="expires" content="-1">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="/dist/img/icon.png" type="image/x-icon">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="/dist/plugins/bootstrap/css/bootstrap.min.css">
    <!--字体-->
    <link rel="stylesheet" href="/dist/css/font-awesome.min.css">
    <!--图标-->
    <link rel="stylesheet" href="/dist/css/ionicons.min.css">
    <!--插件样式-->
    <link href="/dist/plugins/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/dist/plugins/webuploader-0.1.5/webuploader.css">
    <!--OA主样式-->
    <link rel="stylesheet" href="/dist/css/OA.min.css?t=86e2297f">
    <link rel="stylesheet" href="/dist/css/skins/OA-skins.min.css?t=222aea35">
</head>
<body>

<section class="content text-center" id="app">
    <div class="box-body no-padding box-border" style="overflow: auto;background: #f7f8fa;">
        <ul class="ztree" id="ulRight"></ul>
    </div>
    <input type="button" value="保存" id="btnSave" class="btn oaBtn btn-sm"
           style="margin-top:10px;">
    <span id="editInfo" style="color: #999999;position: absolute;right: 10px;bottom: 10px;"></span>
</section>
<script src="https://file.ggxqce.com/web/jquery-2.2.3.min.js"></script>
<!--插件JS-->
<script src="https://file.ggxqce.com/web/bootstrap.min.js"></script>
<!--框架已定义 JS-->
<script src="/dist/js/app_iframe.js?t=8e743a31"></script>
<!--oa自定义  JS-->
<script src="/dist/js/oa.js?t=54c6b89c"></script>

<script src="https://file.ggxqce.com/web/vue.min.js"></script>
<!--ztree组件所需js-->
<script type="text/javascript" src="/dist/plugins/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="/dist/plugins/ztree/js/jquery.ztree.exhide-3.5.js"></script>
<script type="text/javascript">
    var zTreeRight_setting = {
        view: {
            showIcon: function (treeId, treeNode) {
                return treeNode.type == "root" || treeNode.type == "rightFolder";
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            chkboxType: { "Y": "p", "N": "s" },
            enable: true
        }
    };
    var zTreeRegion_setting = {
        view: {
            showIcon: function (treeId, treeNode) {
                return treeNode.isParent;
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            chkboxType: { "Y": "ps", "N": "ps" },
            enable: true
        }
    };
    var zTreeRight = null;
    var zTreeRegion = null;
</script>
<script type="text/javascript">
    $(function () {
        getLastEdit();
        //现有的权限数组
        var currentClickId = ''
        var selectedAllRightIds = []
        var selectedAllRightNames = []
        $.wyui.postMethod(urlConfig.system.right.getTreeNodes, {}, function (nodes) {
            zTreeRight = $.fn.zTree.init($("#ulRight"), zTreeRight_setting, nodes);
            zTreeRight.getNodesByFilter(function (node) {
                if(node.defaultFlag == 1) {
                    zTreeRight.checkNode(node, true, false, false);
                    node.chkDisabled = true;
                    node.nochecked = true;
                }
            });

            $.wyui.postMethod(urlConfig.system.right.getRoleRightByUserId,{userId:$.wyui.page.urlParams.userId},function (rights) {
                for (var i = 0; i < rights.length; i++) {
                    var node = zTreeRight.getNodeByParam("id",rights[i].id);
                    zTreeRight.checkNode(node, true, false, false);
                    node.chkDisabled = true;
                    node.nochecked = true;
                }
            });

            var selectedRightIds = []
            var selectedRightNames = []
            $.wyui.postMethod(urlConfig.system.user2Right.getByUserId,{userId:$.wyui.page.urlParams.userId},function (list) {
                for (var i = 0; i < list.length; i++) {
                    var node = zTreeRight.getNodeByParam("id",list[i].rightId);
                    zTreeRight.checkNode(node, true, false, false);
                    selectedRightIds.push(node.id)
                    selectedRightNames.push(node.name)
                }
            });

            var selectedRightOpIds = []
            var selectedRightOpNames = []
            $.wyui.postMethod(urlConfig.system.user2RightOp.getByUserId,{userId:$.wyui.page.urlParams.userId},function (list) {
                for (var i = 0; i < list.length; i++) {
                    var node = zTreeRight.getNodeByParam("id",list[i].rightOpId);
                    zTreeRight.checkNode(node, true, false, false);
                    selectedRightOpIds.push(node.id)
                    selectedRightOpNames.push(node.name)
                }

                selectedAllRightIds = selectedRightIds.concat(selectedRightOpIds)
                selectedAllRightNames = selectedRightNames.concat(selectedRightOpNames)
                //console.log("权限勾选了"+selectedRightIds.length+"个 : "+selectedRightNames)

            });

        });


        $("#btnSave").on("click",function () {
            var rightIds = new Array();
            var rightOpIds = new Array();

            var rightNames = new Array();
            var rightOpNames = new Array();

            var rightNodes = zTreeRight.getCheckedNodes(true);
            for (var i = 0; i < rightNodes.length; i++) {
                if(rightNodes[i].type == "right" || rightNodes[i].type == "rightFolder"){
                    rightIds.push(rightNodes[i].id);
                    rightNames.push(rightNodes[i].name);
                }else if(rightNodes[i].type == "rightOp"){
                    rightOpIds.push(rightNodes[i].id);
                    rightOpNames.push(rightNodes[i].name);
                }
            }
            $.wyui.postMethod(urlConfig.system.user2Right.config,{
                userId:$.wyui.page.urlParams.userId,
                rightIds:rightIds.join(",")
            },function (data) {
                    //记录日志
                    var allRightIds = rightIds.concat(rightOpIds)
                    var allRightNames = rightNames.concat(rightOpNames)

                    //判断修改类型
                    var beforeIds = selectedAllRightIds.join(",")
                    var beforeNames = selectedAllRightNames.join(",")
                    var afterIds = allRightIds.join(",")
                    var afterNames = allRightNames.join(",")


                    if (beforeIds == afterIds) {
                        //未改动
                        return
                    }

                    var type = 1
                    var remark = ''
                    if (beforeIds.length < afterIds.length) {
                        //新增
                        type = 0
                        remark = selectedAllRightNames.concat(allRightNames).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || allRightNames.indexOf(v) === -1
                        }).join(",")
                    } else if (beforeIds.length == afterIds.length) {
                        //修改
                        type = 1
                        //取交集
                        var common = beforeNames.filter(function (v) {
                            return afterNames.indexOf(v) !== -1
                        })
                        var diff1 = selectedAllRightNames.concat(common).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || allRightNames.indexOf(v) === -1
                        }).join(",")
                        var diff2 = allRightNames.concat(common).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || allRightNames.indexOf(v) === -1
                        }).join(",")
                        remark = diff1 + ",修改为:" + diff2
                    } else {
                        //删除
                        type = 2
                        remark = selectedAllRightNames.concat(allRightNames).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || allRightNames.indexOf(v) === -1
                        }).join(",")
                    }

                    $.wyui.postMethod(urlConfig.system.roleRightConfigLog.saveUser, {
                        createUserId: getCurrentUser().id,
                        roleNameId: getCurrentUser().roleNameId,
                        changeBeforeIds: selectedAllRightIds.join(","),
                        changeBeforeNames: selectedAllRightNames.join(","),
                        changeAfterIds: allRightIds.join(","),
                        changeAfterNames: allRightNames.join(","),
                        changeType: type,
                        remark: remark,
                        userId:$.wyui.page.urlParams.userId,
                        rightType:'1'
                    }, function () {
                        $.wyui.postMethod(urlConfig.system.user2RightOp.config,{
                            userId:$.wyui.page.urlParams.userId,
                            rightOpIds:rightOpIds.join(",")
                        },function (data) {
                            alert('保存成功');
                            var api = frameElement.api, W = api.opener;
                            api.close();
                        });
                    });

            });

        });
    });
    function getLastEdit() {
        //获取最后一次操作日志
        $.wyui.postMethod(urlConfig.system.roleRightConfigLog.getUserLogList, {
            userId:$.wyui.page.urlParams.userId,
            requestPage: 1,
            pageSize: 1
        }, function (data) {
            if (data.records && data.records.length > 0) {
                var log = data.records[0]
                var type = ""
                if (log.changeType == 0) type = "权限: "
                if (log.changeType == 1) type = "将 "
                if (log.changeType == 2) type = "权限: "
                var info = "岗位: " + log.roleName + "\n操作: " + type + log.remark
                $("#editInfo").html("最后由 <b>" + log.createUserName + "</b> 编辑于 <b>" + log.createTime + "</b>&nbsp;&nbsp;<i class='fa fa-question-circle-o'></i>");
                $("#editInfo").attr("title", info)
            }
        });
    }
</script>
</body>
</html>
